#include "Mysocket.h"
#include "Command.h"
#include "Myuser.h"

void showCOMMAND(){
    printf("Command:\n");
    printf("Command Type:\"send\",\"tell\", \"exit\",\"getu\",\"addf\",\"schf\",\"delf\",\"file\"\n");
    printf("Example : send UID Message\n");
    printf("Example : tell UID\n");
    printf("Example : file UID filename\n");
    printf("Example : addf UID\n");
    printf("Example : schf UID\n");
    printf("Example : delf UID\n");  
    printf("          exit\n");
    printf("          getu\n");
}

void ParseCommand2(char *s)
{
    int i, n;
    char type[5], UID[20];
    int connfd;
    int filefd, cnt;
    SAI addr;
    pthread_t ptid;
    
    for (i = 0; i < 4; i++)
        type[i] = s[i];
    type[i] = 0;
    if (strcmp(type, "getu") == 0)
    {
        HeadMsg.cmd = ONLINE_LIST;
        HeadMsg.length = 3;
        WriteMsg(sockfd, &HeadMsg, buf);
        return;
    }
    for (i = 5; s[i] && s[i] != ' '; i++)
        UID[i - 5] = s[i];
    UID[i - 5] = 0;
    if (strcmp(type, "tell") == 0)
    {
        HeadMsg.cmd = P2P_REQUST;
        sprintf(buf, "%s %s", myUID, UID);
        HeadMsg.length = strlen(buf) + 1;
        WriteMsg(sockfd, &HeadMsg, buf);
    }
    else if (strcmp(type, "send") == 0)
    {
        sprintf(buf, "%s %s %s", myUID, UID, s + i + 1);
        connfd = getFd(UID);
        if (connfd < 0)
        {
            HeadMsg.cmd = P2P_TRANS_MSG;
            connfd = -connfd;
        }
        else
            HeadMsg.cmd = P2P_MASSAGE;
        printf("fd = %d\n", connfd);
        HeadMsg.length = strlen(buf) + 1;
        WriteMsg(connfd, &HeadMsg, buf);
    }
    else if (strcmp(type, "addf") == 0)
    {
        HeadMsg.cmd = FRIEND_CTL;
        HeadMsg.error = 1;
        sprintf(buf, "%s %s", myUID, UID);
        HeadMsg.length = strlen(buf)+1;
        WriteMsg(sockfd, &HeadMsg, buf);
    }
    else if (strcmp(type, "schf") == 0)
    {
        HeadMsg.cmd = FRIEND_CTL;
        HeadMsg.error = 0;
        sprintf(buf, "%s %s", myUID, UID);
        HeadMsg.length = strlen(buf)+1;
        WriteMsg(sockfd, &HeadMsg, buf);
    }
    else if (strcmp(type, "delf") == 0)
    {
        HeadMsg.cmd = FRIEND_CTL;
        HeadMsg.error = 2;
        sprintf(buf, "%s %s", myUID, UID);
        HeadMsg.length = strlen(buf)+1;
        WriteMsg(sockfd, &HeadMsg, buf);
    }
    else if (strcmp(type, "file") == 0)
    {
        sprintf(buf, "%s %s %s", myUID, UID, s + i + 1);
        connfd = getFd(UID);
        if (connfd < 0)
        {
            HeadMsg.cmd = P2P_TRANS_FILE;
            connfd = -connfd;
        }
        else
            HeadMsg.cmd = P2P_FILE;
        HeadMsg.error = 0;
        HeadMsg.length = strlen(buf) + 1;
        WriteMsg(connfd, &HeadMsg, buf);
        
        filefd = open(s + i + 1, O_RDONLY);
        cnt = 0;
        while (n = read(filefd, buf, sizeof(buf)))
        {
            HeadMsg.error = ++cnt;
            HeadMsg.length = n;
            WriteMsg(connfd, &HeadMsg, buf);
        }
        HeadMsg.error = maxInt;
        strcpy(buf, "OK");
        HeadMsg.length = strlen(buf) + 1;
        WriteMsg(connfd, &HeadMsg, buf);
    }
    else if (strcmp(type, "exit") == 0)
    {
        HeadMsg.cmd = LOGOUT;
        strcpy(buf, myUID);
        HeadMsg.length = strlen(buf) + 1;
        WriteMsg(sockfd, &HeadMsg, buf);
        printf("Waiting for exiting...\n");
    }
    else
    {
        printf("Wrong Command!\n");
    }
}
